package Controller;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import Entity.Message;
import Service.MessageService;

/**
 * Servlet implementation class CheckMessageServlet
 */
@WebServlet("/CheckMessageServlet")
public class CheckMessageServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public CheckMessageServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		String mid=request.getParameter("mid");
		String mname=request.getParameter("mname");
		String op=request.getParameter("op");
		MessageService ser=new MessageService();
		if("del".equals(op)){
			String id=request.getParameter("id");
			Message m=new Message();
			int tmp=-1;
			if(id!=null&&id!="") tmp=Integer.parseInt(id);
			m.setId(tmp);
			ser.deleteMessage(m);
		}else if("read".equals(op)){
			String id=request.getParameter("id");
			Message m=new Message();
			int tmp=-1;
			if(id!=null&&id!="") tmp=Integer.parseInt(id);
			m=ser.findById(tmp);
			m.setStatus(1);
			ser.updateMessage(m);
		}
		
		List<Message>list=ser.findAllMessage();
		request.setAttribute("list", list);
		request.setAttribute("mid", mid);
		request.setAttribute("mname", mname);
		request.getRequestDispatcher("/WEB-INF/page/CheckMessage.jsp").forward(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

}
